<?php
/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:
 * @LastEditors: liuhongbin
 * @LastEditTime: xxx
 */

namespace app\controller\adminapi\platform;

use app\common\model\attachment\Attachment;
use app\common\model\attachment\AttachmentGroup;
use app\common\model\diy\Diy;
use app\common\model\douyin\DouyinUser;
use app\common\model\platform\PlatformSetting;
use app\common\model\platform\PlatformWechat;
use app\common\model\platform\PlatformWxapp;
use app\common\model\user\User;
use app\common\model\user\UserPermission;
use app\common\model\visit\Visit;
use app\common\model\visit\VisitIp;
use app\common\model\wechat\WechatOrder;
use app\common\model\wechat\WechatRefund;
use app\common\model\wechat\WechatShare;
use app\common\model\wechat\WechatShareReceiver;
use app\common\model\wechat\WechatTransfer;
use app\common\model\wechat\WechatUser;
use app\common\model\wechat\WechatUserWallet;
use app\common\model\wechat\WechatUserWalletLog;
use app\common\model\wework\WeworkApps;
use app\common\model\wework\WeworkKfMsg;
use app\common\model\wework\WeworkKfServicer;
use app\common\model\wework\WeworkKfUser;
use hiduoke\services\FormBuilder as Form;
use think\App;
use app\common\model\platform\Platform;

class Index extends PlatformController
{
    /**
     * @var Platform
     */
    public $model;

    public function __construct(App $app, Platform $model)
    {
        parent::__construct($app);
        $this->model = $model;
    }

    /**
     * 显示平台列表
     * @Route("platform", method="GET")
     * @return \think\Response
     */
    public function index()
    {
        $mix = ['withJoin' => ['user', 'left']];
        $is_del = input('is_del', 0);
        if ($is_del) {
            $model = $this->model->mixPageListModel($this->model->getPageListModel(), $mix)->onlyTrashed();
            $data = $model->append(['type_name', 'type_mark', 'details', 'user'])->select();
            $count = $model->count();
        } else {
            $data = $this->model->getPageListData($mix)->append(['type_name', 'type_mark', 'details', 'user']);
            $count = $this->model->getPageListCount($mix);
        }

        return $this->success($count, $data);
    }

    /**
     * 显示指定的平台
     * @Route("platform/id", method="GET")
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        $row = $this->model->getPlatformByPlaid($id);
        if ($row) {
            return $this->success("ok", $row);
        } else {
            return $this->error("平台不存在");
        }
    }

    /**
     * 删除指定平台
     * @Route("platform/id", method="DELETE")
     * @return \think\Response
     */
    public function delete()
    {
        $id = $this->request->post('id', 0);
        $row = $this->model->where(['plaid' => $id])->find();
        if (!$row) {
            return $this->error("平台不存在");
        }
        $row->delete();
        return $this->success("删除成功");
    }

    /**
     * 真实删除指定平台
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function destroy()
    {
        if (!$this->userInfo['is_admin']) {
            return $this->error("您没有权限");
        }
        $plaid = $this->request->post('id', 0);
        $row = $this->model->onlyTrashed()->where(['plaid' => $plaid])->find();
        if (!$row) {
            return $this->error("平台不存在");
        }
        Attachment::where(['plaid' => $plaid])->delete();
        AttachmentGroup::where(['plaid' => $plaid])->delete();
        Diy::where(['plaid' => $plaid])->delete();
        DouyinUser::where(['plaid' => $plaid])->delete();
        \app\common\model\platform\PlatformAddon::where(['plaid' => $plaid])->delete();
        PlatformSetting::where(['plaid' => $plaid])->delete();
        PlatformWechat::where(['plaid' => $plaid])->delete();
        PlatformWxapp::where(['plaid' => $plaid])->delete();
        UserPermission::where(['plaid' => $plaid])->delete();
        Visit::where(['plaid' => $plaid])->delete();
        VisitIp::where(['plaid' => $plaid])->delete();
        WechatOrder::where(['plaid' => $plaid])->delete();
        WechatRefund::where(['plaid' => $plaid])->delete();
        WechatShare::where(['plaid' => $plaid])->delete();
        WechatShareReceiver::where(['plaid' => $plaid])->delete();
        WechatTransfer::where(['plaid' => $plaid])->delete();
        WechatUser::where(['plaid' => $plaid])->delete();
        WechatUserWallet::where(['plaid' => $plaid])->delete();
        WechatUserWalletLog::where(['plaid' => $plaid])->delete();
        WeworkApps::where(['plaid' => $plaid])->delete();
        WeworkKfMsg::where(['plaid' => $plaid])->delete();
        WeworkKfServicer::where(['plaid' => $plaid])->delete();
        WeworkKfUser::where(['plaid' => $plaid])->delete();
        $row->force()->delete();
        event('delete_platform', $plaid);
        return $this->success("清除成功", $row);
    }

    /**
     * 恢复指定平台
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function recovery()
    {
        if (!$this->userInfo['is_admin']) {
            return $this->error("您没有权限");
        }
        $id = $this->request->post('id', 0);
        $row = $this->model->onlyTrashed()->where(['plaid' => $id])->find();
        if (!$row) {
            return $this->error("平台不存在");
        }
        $row->restore();
        return $this->success("恢复成功", $row);
    }

    //禁用启用
    public function status()
    {
        $id = $this->request->post('id', 0);
        $row = $this->model->where(['plaid' => $id])->find();
        if (!$row) {
            return $this->error("平台不存在");
        }
        $row->status = $row->status == 1 ? 0 : 1;
        $row->save();
        return $this->success("更新成功");
    }


    /**
     * 显示编辑微信平台表单页.
     * @Route("wechat/id/edit", method="GET")
     * @param int $id
     * @return \think\Response
     */
    public function editUserForm($id)
    {
        $rule = array();
        //创建表单
        $platform = new Platform();
        $row = $platform->where(['plaid' => $id])->find();
        if (!$row) {
            return $this->error("当前平台不存在");
        }
        $form = Form::createForm("/adminapi/platform/saveUser")->setMethod("POST");
        //新增表单
        $rule[] = Form::hidden("plaid", $id);
        $rule[] = Form::radio("type", "查询方式", 0)
            ->options([['value' => 0, 'label' => '昵称'], ['value' => 1, 'label' => '用户ID']])
            ->appendControl(0, [Form::dkInput('username', '用户昵称', "", "填写用户昵称，填写后当前平台将转移到此用户名下")->required()])
            ->appendControl(1, [
                Form::dkInputNumber("uid", "用户UID", "", "填写用户的UID，填写后当前平台将转移到此用户名下")->required(),
            ]);
        $form->setTitle("变更所属人")->setRule($rule);
        return $this->success("", Form::getFormData($form));
    }

    public function saveUser()
    {
        $formData = input('formData', []);
        if ($formData['type'] == 0) {
            $user = (new User())->where(['username' => $formData['username']])->find();
        } else {
            $user = (new User())->where(['uid' => $formData['uid']])->find();
        }
        if (!$user) {
            return $this->error("用户不存在");
        }
        $platform = (new Platform())->where(['plaid' => $formData['plaid']])->find();
        if (!$platform) {
            return $this->error("平台不存在");
        }
        $platform->uid = $user['uid'];
        $platform->save();
        return $this->success("变更成功");
    }
}
